package com.house365.core.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;

import com.baidu.mobstat.StatService;
import com.house365.app.analyse.HouseAnalyse;
import com.house365.app.analyse.data.AnalyseMetaData;
import com.house365.core.R;
import com.house365.core.action.ActionTag;
import com.house365.core.anim.AnimBean;
import com.house365.core.application.BaseApplication;
import com.house365.core.constant.CorePreferences;
import com.house365.core.view.LoadingDialog;

/**
 * @author py
 * @date 2012-5-23
 * @comment
 */
public abstract class BaseListActivity extends ListActivity {
	protected LoadingDialog loadingDialog;
	protected BaseApplication mApplication;
	protected AlertDialog.Builder alertDialog;
	protected Activity thisInstance;
	private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
				CorePreferences.DEBUG("onReceive: " + intent);
			finish();
		}
	};

	protected void onCreate(android.os.Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			thisInstance = this;
			mApplication = (BaseApplication) getApplication();
			registerReceiver(mLoggedOutReceiver, new IntentFilter(
					ActionTag.INTENT_ACTION_LOGGED_OUT));

			preparedCreate(savedInstanceState);
			initView();
			initData();
	}

	protected abstract void preparedCreate(android.os.Bundle savedInstanceState);
	protected abstract void initView();
	protected abstract void initData();
	protected void onDestroy() {
		super.onDestroy();
//		mApplication.onAppCancel();
		unregisterReceiver(mLoggedOutReceiver);
		clean();
	}

	protected abstract void clean();

	private LoadingDialog getLoadingDialog() {
		if (loadingDialog == null) {
			loadingDialog = new LoadingDialog(this, R.style.dialog,
					R.string.loading);
			loadingDialog.setCancelable(false);
			loadingDialog.setOnCancelListener(new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					thisInstance.finish();
				}
			});
		}
		return loadingDialog;
	}

	public void showLoadingDialog() {
		if (getLoadingDialog() != null) {
			getLoadingDialog().show();
		}
	}

	public void dismissLoadingDialog() {
		if (getLoadingDialog() != null) {
			getLoadingDialog().dismiss();
		}
	}

	public AlertDialog.Builder getAlertDialog() {
		if (alertDialog == null) {
			alertDialog = new AlertDialog.Builder(this);
		}
		return alertDialog;
	}
	AnalyseMetaData analyseMetadata ;
	protected void onResume() {
		super.onResume();
		if(CorePreferences.getInstance(this).getCoreConfig().isAnalyse()){
			analyseMetadata = HouseAnalyse.onPageResume(this);
		}	
		if(CorePreferences.getInstance(this).getCoreConfig().isOpenBaiduStat()){
			StatService.onResume(this);
		}
	}
	
	protected void onPause() {
		super.onPause();
		if(CorePreferences.getInstance(this).getCoreConfig().isAnalyse()){
			HouseAnalyse.onPagePause(analyseMetadata);
		}	
		if(CorePreferences.getInstance(this).getCoreConfig().isOpenBaiduStat()){
			StatService.onPause(this);
		}
	}
	@Override
	public void finish() {
		super.finish();
		AnimBean animBean = getFinishAnim();
		if(animBean!= null && animBean.getIn()>0 && animBean.getOut()>0){
			overridePendingTransition(animBean.getIn(),animBean.getOut());
		}
		
	}
	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);
		AnimBean animBean = getStartAnim();
		if(animBean!= null && animBean.getIn()>0 && animBean.getOut()>0){
			overridePendingTransition(animBean.getIn(),animBean.getOut());
		}
	}
	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		// TODO Auto-generated method stub
		super.startActivityForResult(intent, requestCode);
		AnimBean animBean = getStartAnim();
		if(animBean!= null && animBean.getIn()>0 && animBean.getOut()>0){
			overridePendingTransition(animBean.getIn(),animBean.getOut());
		}
	}
	public AnimBean getStartAnim() {
//		return new AnimBean(R.anim.slide_in_bottom, R.anim.slide_fix);
	return new AnimBean(R.anim.slide_in_right, R.anim.slide_fix);
}

public AnimBean getFinishAnim() {
		//return new AnimBean(R.anim.slide_fix, R.anim.slide_out_bottom);
	return new AnimBean(R.anim.slide_fix, R.anim.slide_out_right);
}
}
